package com.cwvs.helper;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

import org.springframework.stereotype.Component;

/**
 * 文件路径帮助类
 */
@Component
public class FileRootHelper {
	
	//	imageFileRoot 绝对路径
	private String imageFileRoot;

	//	压缩文件保存的绝对路径
	private String compressionFileRoot;
	
	//	解压后文件保存的绝对路径
	private String decompressionFileRoot;
	
	//GraphicsMagick 路径配置
	private  String graphicsMagickPath;
	
	public String getImageFileRoot() {
		return imageFileRoot;
	}

	public void setImageFileRoot(String imageFileRoot) {
		this.imageFileRoot = imageFileRoot;
	}

	public String getCompressionFileRoot() {
		return compressionFileRoot;
	}

	public void setCompressionFileRoot(String compressionFileRoot) {
		this.compressionFileRoot = compressionFileRoot;
	}

	public String getDecompressionFileRoot() {
		return decompressionFileRoot;
	}

	public void setDecompressionFileRoot(String decompressionFileRoot) {
		this.decompressionFileRoot = decompressionFileRoot;
	}

	/**
	 * 资源文件的绝对路径,资源文件包括:js,css,静态html,不包括图片
	 * 
	 * @param relativeUrl 相对路径
	 * @return
	 */
	public String imageFilepath(String relativeUrl) {
		StringBuilder ret = new StringBuilder();
		ret.append(getImageFileRoot());
		ret.append(relativeUrl);
		return ret.toString();
	}
	 /**
     * (上传或下载文件)的保存目录位置
     * 
     * @param uuid
     * @param type					1上传,2根据excel模板下载,3根据excel下载,4根据压缩文件下载,5上传zip格式文件,其他
     * @param flag
     * @return
     */
    public String genrateUploadAndDownloadFileFloder(String uuid , int type , Boolean flag){
    	if(null == flag){
    		flag = true;
    	}
    	
    	Calendar calendar = Calendar.getInstance();
    	String hour = calendar.get(Calendar.HOUR_OF_DAY) + "";
    	
    	if(hour.length() == 1){
    		hour = "0" + hour;
    	}
    	
    	String typeStr = "";
    	switch (type) {
		case 1:
			typeStr = "upload/";
			break;
		case 2:
			typeStr = "download_by_excel_template/";
			break;
		case 3:
			typeStr = "download_by_excel/";
			break;
		case 4:
			typeStr = "download_by_zip/";
			break;
		case 5:
			typeStr = "upload_by_compress/";
			break;
		default:
			typeStr = "other/";
			break;
		}
    	
    	String path = "";
    	if(flag){
    		if(null == uuid || uuid.trim().length() == 0){
        		uuid = UUID.randomUUID().toString();
        	}
    		path = handleFolderPath(getCompressionFileRoot()) + typeStr + new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()) + "/" + hour + "/" + uuid + "/";
    	}else{
    		path = handleFolderPath(getCompressionFileRoot()) + typeStr + new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()) + "/" + hour + "/";
    	}
    	
    	File file = new File(path);
    	if(!file.exists()){
    		file.mkdirs();
    	}
    	return path;
    }
    /**
	 * 处理文件夹的路径
	 * 
	 * @param fileName
	 * @return
	 */
	public String handleFolderPath(String fileName){
        if(null != fileName && fileName.trim().length() > 0){
            fileName = fileName.replace("\\", "/");
            if(!fileName.endsWith("/")){
                fileName += "/";
            }
        }
        return fileName;
    }

	public String getGraphicsMagickPath() {
		return graphicsMagickPath;
	}

	public void setGraphicsMagickPath(String graphicsMagickPath) {
		this.graphicsMagickPath = graphicsMagickPath;
	}

}
